﻿using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Phone.Controls;
using DBHelper.DB;

namespace PhoneUI.Pages
{
    public partial class Setting : PhoneApplicationPage
    {
        bool ipchange = false;
        bool portchange = false;

        Controls.AddNewOrder ano;

        public Setting()
        {
            InitializeComponent();
            DBHelper.DB.PhoneData db = new PhoneData(PhoneData.connectString);
            var ip = db.Setting.SingleOrDefault<SettingTable>(s => s.SettingName == "IpAddress");
            if (ip != null)
                IpBox.Text = ip.SettingValue;
            var port = db.Setting.SingleOrDefault<SettingTable>(s => s.SettingName == "Port");
            if (port != null)
                PortBox.Text = port.SettingValue;
            db.Dispose();
        }

        private void IpBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            ipchange = true;
        }

        private void PortBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            portchange = true;
        }

        private void IpBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if(VeriIp(IpBox.Text.Trim()))
                if (ipchange)
                {
                    PhoneData db = new PhoneData(PhoneData.connectString);
                    var editip = db.Setting.SingleOrDefault<SettingTable>(s => s.SettingName == "IpAddress");
                    if (editip == null)
                    {
                        MessageBox.Show("保存失败，数据库出错!");
                        return;
                    }
                    editip.SettingValue = IpBox.Text.Trim();
                    db.SubmitChanges();
                    db.Dispose();
                    ipchange = false;
                }
        }

        private void PortBox_LostFocus(object sender, RoutedEventArgs e)
        {
            int port;
            if(VeriPort(PortBox.Text.Trim(),out port))
                if (portchange)
                {
                    PhoneData db = new PhoneData(PhoneData.connectString);
                    var editport = db.Setting.SingleOrDefault<SettingTable>(s => s.SettingName == "Port");
                    if (editport == null)
                    {
                        MessageBox.Show("保存失败，数据库出错!");
                        return;
                    }
                    editport.SettingValue = port.ToString();
                    db.SubmitChanges();
                    db.Dispose();
                    portchange = false;
                }
        }

        //验证IP格式
        private bool VeriIp(string ip)
        {
            bool result = false;
            IPAddress ipa;
            if (IPAddress.TryParse(ip, out ipa))
            {
                result = true;
            }
            else
            {
                MessageBox.Show("请输入正确的IP地址！");
            }
            return result;
        }

        //验证端口格式
        private bool VeriPort(string port, out int Num)
        {
            bool result = false;
            int output;
            if (int.TryParse(port, out output))
            {
                result = true;
            }
            else
            {
                MessageBox.Show("输入的端口格式不正确");
            }
            Num = output;
            return result;
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            
            if (ano.IsOpen)
            {
                e.Cancel = true;
                ano.IsOpen = false;
            }
        }

        private void appbar_button2_Click(object sender, EventArgs e)
        {
            if (ano != null)
                ano.IsOpen = false;
            this.NavigationService.GoBack();
        }

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ano = new Controls.AddNewOrder();
            ano.OnSure += new Controls.NewOrderEventHandler(ano_OnSure);
            ano.Show();
        }

        void ano_OnSure(object sender, Controls.NewOrderEventArgs e)
        {
            MessageBox.Show(e.ImagePath);
        }

        
    }
}